home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt0187b.arc / RELXH.TXT < prev    next >
Text File  |  1985-02-05  |  12KB  |  229 lines

  1. About the Programs
  2.    Listings 1 and 2 enable you to experiment with the relaxation
  3. method if you have a computer that runs Microsoft BASIC. These
  4. listings, as is, run on an Apple II computer in 40-column mode, but only
  5. the subroutines at lines 10000 and 14000 in listing 1 and lines 10000
  6. and 23000 in listing 2 (which implement file input from and
  7. output to disk) must be changed to get this program to work on
  8. machines like the Radio Shack Model IV, the IBM Personal Computer, the
  9. Commodore 64 and VIC, and other computers.
  10.    You may want to change the print-array routines at line 20000
  11. in each program to display data in the best way for your com
  12. puter; these routines were written to display data neatly on an
  13. 80-column printout. All REM statements can be removed, and
  14. all variables can be shortened to their first two characters. Variable
  15. names are sometimes spelled oddly; this is to ensure that they don't
  16. conflict with BASIC reserved words and other variables. (In Applesoft
  17. and some other Microsoft BASICs, only the first two characters
  18. of a variable name are remembered.)  In addition, try larger array
  19. sizes for the DIM (dimension) statements of both programs. Your
  20. computer may have more space available than mine does.
  21.    Listing 1 is the program I call EDITOR. With this program, you
  22. can create a new data file or modify a data file; modifications
  23. include changing elements, changing the size of the array, or expanding
  24. the array to twice its size. Listing 2, the program called
  25. RELAXN, reads this data file, does the relaxation in a semiautomated
  26. fashion, prints intermediate and final results, and enables you to
  27. save the final result onto disk for later manipulation.
  28.    The data file used by both programs has the following contents:
  29. first, the number of rows in the input array; second, the number
  30. of columns; third, a numeric value that represents an inactive node
  31. (called the "inactive number"); and, finally, the elements of the
  32. input array listed by rows.
  33.    The input array needs some explanation because it is used to
  34. represent several different kinds of data. I created the inactive
  35. number so these programs could work with rounded cross sections.
  36. The inactive number can be any value not otherwise found in the
  37. input array. You should use it only in the corners of the array
  38. to make a rounded shape fit into a rectangular array. The second
  39. kind of data is the boundary elements. The RELAXN program looks
  40. at the input array and flags the outermost layer of numbers (ignoringinactive nodes, if any) as boundary elements. The third
  41. kind of data is any elements left; these represent the initial values
  42. of the interior nodes in the cross section.
  43.    The RELAXN program reads the input array into the NODE array
  44. and, before manipulating it, creates a same-sized MASK array that
  45. stores the type of each element. Inactive elements have a MASK
  46. value of -1, boundary elements have a value of 0, and interior
  47. elements have a MASK of 1. The program checks this array often
  48. to prevent doing inappropriate operations on any given element.
  49.    The notes for listing 1 provide a commentary on the EDITOR pro-
  50. gram, which is pretty straightforward. You can start a data
  51. file from scratch or read in a previously existing one. You can
  52. change the array by row, column, or individual element. You can
  53. change the size of an array loaded in from disk and also expand
  54. an array to twice its size (actually, from m-by-n to (2m-1)-by-(2n-1)).
  55. This option, discussed in the main text, is used to get
  56. more accurate results. When you choose to expand the input array,
  57. the computer tries to interpolate the values of added elements in
  58. a context-sensitive way. It usually does a good job, but you should
  59. inspect the resulting array and patch up any flaws.
  60.    The notes for listing 2 provide a commentary on the RELAXN
  61. program. The program reads in the input array, creates the MASK
  62. and RESID arrays, lets you do block relaxations (if desired), repeats
  63. the main loop of the iteration algorithm until the RESID array
  64. is within the specified range of accuracy, prints out the result,
  65. and allows you to save the solved NODE array for later use. The
  66. program also lets you set the number of iterations to be performed
  67. before the NODE and RESID arrays are to be printed, gives you
  68. a warning message if the relaxation "hangs" on a single node
  69. (which sometimes denotes the end of the algorithm at that level
  70. of accuracy before the official criteria for ending are fulfilled), and
  71. lets you abort the algorithm and save your results.
  72.    I wrote this program to be as simple and clear as possible; there
  73. are numerous optimizations I did not perform, leaving that to the
  74. enterprising programmer. These two programs were designed us
  75. ing a structured flowchart format I described three years ago (see
  76. "Structured Programming and Structured Flowcharts,' March
  77. 1981 , page 20). The structured flowcharts were then translated
  78. into BASIC code, whcih accounts for the occasionally unconventional
  79. use of GOTOs in the programs. I reluctantly chose
  80. BASIC over Pascal because BASIC is still the lingua franca of
  81. BYTE readers--a recent study we did showed that 77% of our
  82. readers use BASIC most often, while 21% use some kind of assembly
  83. language, and only 15% use Pascal.
  84.    One final note: I must confess to the use of a quick-and-dirty
  85. shortcut concerning the block relaxation subroutine in RELAXN.
  86. Instead of actually implementing the block relaxation algorithm
  87. (which decreases the number of computations to relax a block of
  88. elements by the same amount), I had the computer execute a double-
  89. nested do-loop that relaxed each element individually. You should
  90. implement the true block relaxation algorithm if you are going
  91. to be doing many large block relaxations. Mea culpa, mea maxima
  92. culpa.
  93.  
  94.  
  95.  
  96. Program Notes for Listing 1
  97.  
  98. Line Group         Function
  99.  
  100.  181-195  Loads in an array from disk; you have the otions
  101.       of expanding the array (line 184) or arbitrarily
  102.       changing its size (line 188).
  103.  200-210  Gets the size of the array (MROWS, MCOLS)
  104.       and the value of the "inactive' element (INACTIVE)
  105.       the array is being built from scratch.
  106.  410-465  Gets a row of values; this section repeats until
  107.       you give it -1 for a row number
  108.  472-486  Gets a column of values; this section repeats
  109.       until you give it -1 for a column number
  110.  505-550  Gets an individual element to change; this section
  111.       repeats until you give it a -1,0,0 to end it
  112.  600-630  Saves the file to disk.
  113.  
  114.  
  115.  Subroutines
  116.  
  117.  10000-10060 Reads data file from disk.
  118.  11000-11500 Expands array A to a twice-sized array B. Does
  119.          interpolation to fill in missing values. If one of
  120.          the two values used for interpolation is the
  121.          inactive element, the value being interpolated is set
  122.          equal to the active element.
  123.  14000-14060 Writes data file to disk.
  124.  20000-20420 Displays the array being worked on (array B).
  125.          Because the array may have more columns
  126.          than can be printed on an 80-column printer, I
  127.          wrote this routine to display C10LPERPAGE columns
  128.          at a time; I can then paste these strips
  129.          together to get the entire array. You may want to
  130.          change the value of C10LPERPAGE or write a
  131.          more efficient, implementation-specific
  132.          subroutine.
  133.  
  134.  
  135.  
  136.  
  137. Program Notes for Listing 2
  138.  
  139.  210-220  Reads input file.
  140.  300      Creates MASK array from input array.
  141.  380      Sets flag MANUAL$, which determines whether
  142.       you can do point relaxations from the keyboard
  143.       after every printout of the NODE and RESID
  144.       arrays.
  145.  400-410  Gets the desired number of decimal places of
  146.       accuracy and computes the values of two error-
  147.       limit values, ERR (maximum error for any one
  148.       element) and ESUM (maximum error for sum of
  149.       all error values).
  150.  500      Calculates the value of the RESID array.
  151.  600-660  Enables you to do block relaxation; this section
  152.       of code repeats until you answer N to the question
  153.       in line 605.
  154.  800      Checks to see if relaxation algorithm is finished.
  155.       If it is (very unlikely), QUIT$ is set to Y.
  156.  900      Gets the number of iterations to be performed,
  157.       ITERLEFT, before the NODE and RESID arrays
  158.       are printed.
  159.  1000-1300 Main loop of program, repeated until QUIT$
  160.       becomes Y. Its main events are doing a point
  161.       relaxation on the element that needs it most (line
  162.       1103), and evaluating RESID for end-of-algorithm
  163.       (setting QUITE$ to Y if the conditions are met--
  164.       line 1200). Each time through this loop,
  165.       ITERLEFT is decremented by 1; if it goes to 0,
  166.       the NODE and RESID arrays are printed (line
  167.       1160), you get a chance to quit the program
  168.       prematurely (if it "hangs" on certain values--line
  169.       1240), and the program gets a new value for
  170.       ITERLEFT (line 1242). In certain circumstances,
  171.       the variation this algorithm gives is too "coarse"
  172.       to adjust the RESID array below the given error
  173.       threshold. This usually results in the program
  174.       relaxing the same node endlessly. The program
  175.       gives you a warning if it detects that the same
  176.       node has been relaxed twice in a row.
  177.  1400-1420 Gives you a chance to do manual relaxation to
  178.       fine-tune the NODE array before saving it to
  179.       disk.
  180.  1500-1530 Recalculates the RESID array from the NODE
  181.       array and rechecks to ensure that the NODE
  182.       array actually meets the terminating conditions.
  183.       The program does this because each relaxation
  184.       adjusts the NODE and RESID arrays, and roundoff
  185.       errors may have accumulated.
  186.  2000-2110 Gives you the option to save the NODE data to
  187.       a disk data file. RESID is not saved because it
  188.       can be directly calculated from NODE.
  189.  
  190.  
  191. Subroutines
  192.  
  193.  15000-15620 Creates MASK array from input array (which is
  194.         contained in the NODE array variable). All non-
  195.         INACTIVE values on the first and last rows are
  196.         considered to be border elements. On all other
  197.         rows, the rows are inspected from the ends
  198.         inward; the first non-INACTIVE value on each end
  199.         is taken to be a border element. All the
  200.         elements framed by the two border elements
  201.         are taken to be active (interior) elements. Inactive
  202.         elements are marked by 1, border elements by 0,
  203.         active elements by 1 in MASK.
  204.  17000-1720 Relaxes node (I,J) by the amount N. The RESID
  205.         values are changed according to the relaxation
  206.         template only if the node is an active, interior
  207.         one; border and inactive elements are not
  208.         changed.
  209.  19000-19210 Does a block relaxation given the upper-left
  210.         corner element (RLO, CLO) and lower-right corner
  211.         element (RHI, CHI). This routine automatically
  212.         calculates the number of units for the block to
  213.         be relaxed in line 19080.
  214.  20000-20420 Prints the NODE and RESID arrays. See the
  215.         reference to line 20000 in the program notes for
  216.         listing 1.
  217.  21000-21200 Evaluates the RESID array to determine if the
  218.         program is finished (see line 21120). If so,
  219.         QUIT$ is set to Y.
  220.  22000-22200 Finds the element with the largest RESID value
  221.         and relaxes it to 0.
  222.  23000-23060 Saves the NODE array and related information
  223.         to disk.  This data file can be read again by
  224.         either the EDITOR or RELAXN programs.
  225.  24000-24080 Enables you to do point relaxations from the
  226.         keyboard.
  227. ain by
  228.         either the EDITOR or RELAXN programs.
  229.  24000-24080 Enables you to do point relaxations from